<template>
  <div class="box">
    <!-- 表头 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="formInline.user"
          placeholder="根据用户名搜索"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="" @click="resetInput">清空</el-button>
        <el-button type="primary" @click="searchName(formInline.user)">搜索</el-button>
      </el-form-item>
      <el-button type="success" class="right" @click="showEdit('add')"
        ><i class="el-icon-edit"></i>新增用户</el-button
      >
      <el-alert
    :title="'共'+`${total}`+'条记录'"
    type="info"
    show-icon>
  </el-alert>
    </el-form>

    <!-- 表格数据 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="序号" width="150"> </el-table-column>
      <el-table-column prop="email" label="邮箱" width="150"> </el-table-column>
      <el-table-column prop="phone" label="联系电话"> </el-table-column>
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column
        prop="permission_group_title
"
        label="权限组名称"
      >
      </el-table-column>
      <el-table-column prop="role" label="角色"> </el-table-column>
      <el-table-column prop="address" label="操作">
        <!-- 子组件插槽 -->
        <template #default="{ row }">
          <div class="actions">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              class="bt1"
              @click="showEdit('edit',row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              class="bt2"
              @click="del(row)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryObj.page"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="queryObj.pageSize"
      layout="prev, pager, next,sizes, jumper"
      :total="total"
      background
      class="fpage"
    >
    </el-pagination>
    <!-- dialog -->
    <el-dialog
      :title="Title"
      :visible.sync="centerDialogVisible"
      :before-close="quxiao"
      width="50%"
      center
    >
      <!-- 新增用户表单内容 -->
      <el-form ref="form" :model="form" label-width="200px" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="flag">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-input v-model="form.role"></el-input>
        </el-form-item>
        <el-form-item label="权限组名称">
          <el-select v-model="form.region" placeholder="请选择">
            <el-option label="超级管理员组" value="shanghai"></el-option>
            <el-option label="管理员" value="beijing"></el-option>
            <el-option label="昆昆" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input
            type="textarea"
            v-model="form.introduction"
            placeholder="Please input"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button type="primary" @click="queding">确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, remove, detail, add, update } from '@/api/base/users.js'
export default {
  data () {
    return {
      flag: true,
      centerDialogVisible: false, // 控制dialog的开关
      drawerType: 'add',
      total: 20,
      // Title: '',
      queryObj: {
        page: 1,
        counts: 1,
        pagesize: 10
      },

      tableData: [
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }
      ],
      formInline: {
        user: '',
        region: ''
      },
      // dialog表单内容
      id: 0,
      form: {
        avatar: '',
        username: '',
        email: '',
        password: '',
        role: '',
        // region: '',
        phone: '',
        introduction: '',
        permission_group_id: 0
      },
      // 表单校验
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        email: [{ required: true, message: '邮箱不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.initData()
  },
  methods: {
    // 展示centerDialog窗口
    async showEdit (type, id) {
      this.flag = true
      // console.log(id.id)
      this.drawerType = type
      if (type !== 'add') {
        this.flag = false
        const { data } = await detail(id)
        // console.log(data)
        this.form.avatar = data.avatar
        this.form.email = data.email
        this.id = data.id
        this.form.introduction = data.introduction
        this.form.permission_group_id = data.permission_group_id
        this.form.phone = data.phone
        this.form.role = data.role
        this.form.username = data.username
        // this.form.region = data.region
        // console.log(this.form)
      }
      this.centerDialogVisible = true
      // const newForm = []
    },
    // 关闭dialog
    quxiao () {
      // 清空表单
      this.form.avatar = ''
      this.form.email = ''
      this.form.password = ''
      this.id = 0
      this.form.introduction = ''
      this.form.permission_group_id = 0
      this.form.phone = ''
      this.form.role = ''
      this.form.username = ''
      this.centerDialogVisible = false
    },
    // 封装渲染数据
    async initData () {
      const { data } = await list(this.queryObj)
      // console.log(data)
      this.total = data.counts
      this.tableData = data.list
      // console.log(this.tableData)
    },
    // 删除对应id的数据
    async del (id) {
      // console.log(1)
      try {
        await remove(id)
        this.$message.success('删除成功')
        // console.log(id)
        this.initData()
      } catch (error) {
        this.$message.error('删除失败')
      }
    },

    // 分页
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.queryObj.pagesize = val
      this.initData()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.queryObj.page = val
      this.initData()
    },
    // 搜索功能
    searchName (uname) {
      this.tableData = this.tableData.filter(item => item.username === uname)
    },
    // 清空功能
    resetInput () {
      this.formInline.user = ''
      this.initData()
    },
    // 增加请求
    async queding () {
      try {
        await this.$refs.form.validate()
        if (this.drawerType === 'add') {
          // 发起添加请求
          // console.log(1)
          await add(this.form)
        }
        if (this.drawerType === 'edit') {
          // 发起修改请求
          const res = await update({ id: this.id, ...this.form })
          console.log(res)
        }
        // 清空表单
        this.form.avatar = ''
        this.form.email = ''
        this.form.password = ''
        this.id = 0
        this.form.introduction = ''
        this.form.permission_group_id = 0
        this.form.phone = ''
        this.form.role = ''
        this.form.username = ''
        // 轻提示
        this.$message.success(
          this.drawerType === 'add' ? '添加成功' : '修改成功'
        )
        this.initData()
        this.centerDialogVisible = false
      } catch (error) {
        this.$message.error('请输入必填项')
      }
    }
  },
  computed: {
    Title () {
      if (this.drawerType === 'add') return '创建用户'
      if (this.drawerType === 'edit') return '编辑用户'
      else {
        return 'add'
      }
    }
  }
}
</script>

<style scoped lang="less">
.box {
  background-color: #fff;
  padding: 20px 20px 70px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: relative;
  //编辑、删除按钮样式
  .bt1 {
    background-color: #ecf5ff;
    color: #409eff;
  }
  .bt1:hover {
    background-color: #409eff;
    color: #ecf5ff;
  }
  .bt2 {
    background-color: #ecf5ff;
    color: #f87f92;
  }
  .bt2:hover {
    background-color: #f87f92;
    color: #ecf5ff;
  }
  .right {
    float: right;
  }
  .fpage {
    position: absolute;
    right: 30px;
    bottom: 20px;
  }
}
</style>
